package com.ftg.learn.controller;

import com.ftg.learn.entity.EmployeeEntity;
import com.ftg.learn.service.EmployeeService;
import com.ftg.learn.util.PageEx;
import com.ftg.learn.util.R;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 布衣
 */
@RestController
@Api(tags="员工管理")
@CrossOrigin
public class EmployeeController {

    @Resource
    EmployeeService employeeService;

    /**
     * http 报文  请求报文[发送请求时]  响应报文【发送响应的时候】
     * @return
     */
    @GetMapping("/showAll")
    @ApiOperation("列出全部员工信息")
    public R<PageEx<EmployeeEntity>> showAll(
            @ApiParam(value="姓名") @RequestParam(value = "ename",required = false) String ename,
            @ApiParam(value="页码",required = true,defaultValue = "1") @RequestParam("page") int page,
            @ApiParam(value="每页数量",required = true,defaultValue = "10") @RequestParam("size") int size
    ){
        return employeeService.showAll(ename,page,size);
    }

    @GetMapping("/showAllByName")
    @ApiOperation("根据员工姓名模糊查询")
    public List<EmployeeEntity> showAllByName(
        @ApiParam(value="员工姓名",required = true,defaultValue = "张三") @RequestParam("ename") String ename
    ){
        return employeeService.showAllByName(ename);
    }

    @ApiOperation("添加员工")
    @PostMapping("/add")
    public R<Integer> addEmployee(
            @ApiParam(value="员工信息",required = true) @RequestBody EmployeeEntity employeeEntity
    ){
        return employeeService.addEmployee(employeeEntity);
    }

    @ApiOperation("删除员工")
    @DeleteMapping("/delete/{eid}")
    public R<Integer> deleteEmployee(
            @ApiParam(value="员工编号",required = true) @PathVariable("eid") int eid
    ){
        return employeeService.deleteEmployee(eid);
    }

    @ApiOperation("修改员工信息")
    @PutMapping("/update")
    public R<Integer> updateEmployee(@RequestBody EmployeeEntity employeeEntity){
        return employeeService.updateEmployee(employeeEntity);
    }

    @ApiOperation("批量删除员工信息")
    @DeleteMapping("/deleteByIds")
    public R<Integer> deleteByIds(@RequestParam("eids") List<Integer> eids){
        return employeeService.deleteByIds(eids);
    }


    @ApiOperation("pagehelper配置文件")
    @GetMapping("/pageHelperAll")
    public R<PageEx<EmployeeEntity>> pageHelperAll(
            @RequestHeader("author_token")String token,
            @ApiParam(value="姓名") @RequestParam(value = "ename",required = false) String ename,
            @ApiParam(value="页码",required = true,defaultValue = "1") @RequestParam("page") int page,
            @ApiParam(value="每页数量",required = true,defaultValue = "10") @RequestParam("size") int size
    ){
        System.out.println(token);
        try{
            Jwt o = Jwts.parser().setSigningKey("secret").parse(token);
        }catch (Exception e){
            return R.failure("token错误");
        }

        return employeeService.pageHelperAll(ename,page,size);
    }



    @ApiOperation("pageHelper测试")
    @GetMapping("/pageTest")
    public R<PageInfo<EmployeeEntity>> pageTest(
            @ApiParam(value="姓名") @RequestParam(value = "ename",required = false) String ename,
            @ApiParam(value="页码",required = true,defaultValue = "1") @RequestParam("page") int page,
            @ApiParam(value="每页数量",required = true,defaultValue = "10") @RequestParam("size") int size
    ){
        return employeeService.pageTest(ename,page,size);
    }

//    get select post select| insert  put update  delete delete
}
